#include "scalewidget.h"
#include "ui_scalewidget.h"

ScaleWidget::ScaleWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ScaleWidget)
{
    ui->setupUi(this);
    m_ScaleValueList.append(750);
    m_ScaleValueList.append(500);
    m_ScaleValueList.append(200);
    m_ScaleValueList.append(100);
    m_ScaleValueList.append(50);
    m_ScaleValueList.append(20);
    m_ScaleValueList.append(10);
    m_ScaleValueList.append(5);
    m_ScaleValueList.append(2);
    m_ScaleValueList.append(1);
    m_targetDistance = 0;
    m_targetSpeed = 0;
    target_distance_bar_color = QColor(44,145,51);
    target_distance_num_color = Qt::gray;
    target_speed_num_color = Qt::white;
}

ScaleWidget::~ScaleWidget()
{
    delete ui;
}

void ScaleWidget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.scale(this->width()/150.0,this->height()/500.0);

    drawScales(&painter);
}

inline float ScaleWidget::Ypos(uint8_t index)
{
    if(index > SCALENUM -1)
        return 0;
    float Scaleheight = 35.0;
    float initYpos = 60.0;
    return initYpos+index*Scaleheight+(index/3)*10;
}

void ScaleWidget::drawScales(QPainter *painter)
{
    painter->save();
    //画刻度
    painter->setBrush(SCALECLR);
    painter->setFont(QFont("Arial",15,QFont::Normal));
    for(int i=0;i<SCALENUM;++i)
    {
        painter->setPen(QPen(QPen(Qt::gray,2, Qt::SolidLine)));
        painter->drawLine(QPoint(60,Ypos(i)),QPoint(90,Ypos(i)));
        painter->setPen(QPen(Qt::white,2, Qt::SolidLine));
        if(i==SCALENUM-1)
            painter->drawText(QRect(8,Ypos(i)-10,60,22),Qt::AlignCenter,QString("m %1").arg(m_ScaleValueList.at(i)));
        else
            painter->drawText(QRect(8,Ypos(i)-10,60,22),Qt::AlignCenter,QString("%1").arg(m_ScaleValueList.at(i)));
    }
    //画柱状图
    painter->setPen(QPen(target_distance_bar_color,1, Qt::SolidLine));
    painter->setBrush(target_distance_bar_color);
    if(m_targetDistance == 750)
        painter->drawRect(100,Ypos(0),40,Ypos(SCALENUM-1)-Ypos(0));

    if(m_targetDistance > 0 && m_targetDistance < 750)
    {
        for(int i=0;i<SCALENUM;++i)
        {
            if(m_ScaleValueList.at(i) <= m_targetDistance){
                float y= Ypos(i) - (i%3?35:45)*((m_targetDistance-m_ScaleValueList.at(i))/(m_ScaleValueList.at(i-1)-m_ScaleValueList.at(i)));
                painter->drawRect(100.0,y,40.0,Ypos(SCALENUM-1)-y);
                break;
            }
        }
    }

    if(m_targetDistance == 0)
    {
        painter->setPen(QPen(Qt::transparent,1, Qt::SolidLine));
        painter->setBrush(Qt::transparent);
        painter->drawRect(100,Ypos(0),40,Ypos(SCALENUM-1));
    }
    //画距离数字
    painter->setPen(QPen(target_distance_num_color,3, Qt::SolidLine));
    painter->setFont(QFont("Arial",20,QFont::Normal));
    painter->drawText(QRect(0,Ypos(SCALENUM-1),150,50),Qt::AlignCenter,QString("%1 m").arg(m_targetDistance));
    //画目标速度
    painter->setPen(QPen(target_speed_num_color,3, Qt::SolidLine));
    painter->setFont(QFont("Arial",25,QFont::Normal));
    painter->drawText(QRect(0,Ypos(0)-50,150,50),Qt::AlignCenter,QString("%1").arg(m_targetSpeed));
    painter->restore();
}

void ScaleWidget::setDistanceColor(float tar_speed,float tar_distance)
{
    if(tar_distance > 300.0)
        target_distance_bar_color =  QColor(44,145,51);
    else if(tar_distance <= 300.0 && tar_distance >= 150.0)
    {
        if(tar_speed >= 25.0)
            target_distance_bar_color = QColor(44,145,51);
        else if(tar_speed >= 0)
            target_distance_bar_color = Qt::yellow;
    }
    else if(tar_distance < 150.0)
    {
        if(tar_speed >= 60.0)
            target_distance_bar_color = QColor(44,145,51);
        else if(tar_speed > 0)
            target_distance_bar_color = Qt::yellow;
        else if(tar_speed == 0)
            target_distance_bar_color = Qt::red;
    }
    update();
}

void ScaleWidget::setTargetDistance(float distance)
{
    if(distance < 0)
        m_targetDistance = 0;
    else if(distance > 750)
        m_targetDistance = 750;
    else
        m_targetDistance = distance;
    setDistanceColor(m_targetSpeed,m_targetDistance);
    update();
}

void ScaleWidget::setTargetSpeed(float speed)
{
    m_targetSpeed = speed;
    setDistanceColor(m_targetSpeed,m_targetDistance);
    update();
}

void ScaleWidget::setStandByMode(bool set_mode)
{
    if(set_mode)
    {
        target_distance_bar_color = QColor(0,0,0);
        target_distance_num_color = QColor(0,0,0);
        target_speed_num_color = QColor(0,0,0);
    }
    else
    {
        target_distance_bar_color = QColor(44,145,51);
        target_distance_num_color = Qt::gray;
        target_speed_num_color = Qt::white;
    }
    update();
}
